package com.example.sl.database;

import android.app.NativeActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.sl.model.UserInfo;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class UserHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "t_user";
    private static final String TABLE_NAME = "user_info";


    private static final String ID = "id";
    private static final String NAME = "name";
    private static final String AGE = "age";
    private static final String SEX = "sex";
    private static final String ADDR = "addr";

    private static final int VERSON = 1;

    private SQLiteDatabase db;

    public UserHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, VERSON);
    }

    public UserHelper(Context context){
        this(context,DATABASE_NAME,null,VERSON);
        db = this.getWritableDatabase();
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME + " (" +
                ID + " INTEGER PRIMARY KEY," +
                NAME + " TEXT, "
                +AGE + " INTEGER, "
                +SEX +" TEXT, "
                +ADDR +" TEXT " + ")";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insertUser(UserInfo userInfo) {
        ContentValues values = new ContentValues();
        values.put(NAME,userInfo.getName());
        values.put(AGE,userInfo.getAge());
        values.put(SEX,userInfo.getSex());
        values.put(ADDR,userInfo.getAddr());
        return db.insert(TABLE_NAME,null,values);
    }

    public int deleteUser(String name){
        return db.delete(TABLE_NAME,"name=?",new String[]{name});
    }

    public int updateUser(UserInfo userInfo){
        ContentValues values = new ContentValues();
        values.put(NAME,userInfo.getName());
        values.put(AGE,userInfo.getAge());
        values.put(SEX,userInfo.getSex());
        values.put(ADDR,userInfo.getAddr());
        return db.update(TABLE_NAME,values,"name=?",new String[]{userInfo.getName()});
    }

    public List<UserInfo> queryUser(){
        List<UserInfo> userInfos = new ArrayList<>();
        Cursor cursor = db.query(TABLE_NAME, new String[]{ID, NAME, AGE, SEX, ADDR}, null, null, null, null, null);
        while (cursor.moveToNext()){
            UserInfo userInfo = new UserInfo();
            int id = cursor.getInt(0);
            String name1 = cursor.getString(1);
            Integer age = cursor.getInt(2);
            String sex = cursor.getString(3);
            String addr = cursor.getString(4);

            userInfo.setId(id);
            userInfo.setName(name1);
            userInfo.setAge(age);
            userInfo.setAddr(addr);
            userInfo.setSex(sex);

            userInfos.add(userInfo);
        }
        cursor.close();
        return userInfos;
    }

    public void insertTwoUser() {
        try{
            db.beginTransaction();
            for (int i = 0; i < 3; i++) {

                UserInfo userInfo = new UserInfo();
                userInfo.setName("ye"+i);
                userInfo.setAddr("guangzhou"+i);
                userInfo.setAge(12+i);
                userInfo.setSex(i%2==0?"男":"女");

                insertUser(userInfo);
            }
            db.setTransactionSuccessful();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            db.endTransaction();
        }





    }
}
